import {RouterConfig} from "@/config/RouterConfig";
import {Breadcrumb} from "antd";
import {HomeOutlined} from "@ant-design/icons";
import React from "react";
import {HomeBreadcrumbContainer} from "@/style/AppStyle";
import {queryState} from "@/util/global";

/**
 * 导航栏专用方法
 * 返回React Element
 * @param path
 * @param data
 * @returns {JSX.Element}
 */
export function getCurrentRouterConf(path = window.location.pathname, data = RouterConfig) {
    /* 首页不显示导航栏 || 顶栏如果不现实则也隐藏导航栏 */
    if (path === '/' || path === '/home' || !path || !queryState('header.visible')) {
        return null;
    }
    let config = [];
    for (let index in data) {
        let conf = data[index]
        getConfig(path, conf, config);
    }
    const view = config.map(
        (i, index) => {
            return <Breadcrumb.Item
                onClick={() => window._ROUTER_.push(i.path)}
                style={{cursor: 'pointer'}}
                key={index}>
                {i.icon}
                <span>{i.label}</span>
            </Breadcrumb.Item>

        })
    return <HomeBreadcrumbContainer>
        <Breadcrumb>
            <span>当前位置：</span>
            <Breadcrumb.Item key={'home'} onClick={() => window._ROUTER_.push('/home')} style={{cursor: 'pointer'}}>
                <HomeOutlined/>
                <span>首页</span>
            </Breadcrumb.Item>
            {
                view
            }
        </Breadcrumb>
    </HomeBreadcrumbContainer>

}

/** 获取当前path **/
export function getPath() {
    return window._ROUTER_.location.pathname;
}

function getConfig(path, data, config = []) {
    let obj = {
        path: data.path,
        label: data.label,
        icon: data.icon
    }
    if (
        path === data.path &&
        data.exact === true &&
        path !== '/home'
    ) {
        config.push(obj);
        return;
    }
    if (path.startsWith(data.path) &&
        (data['routes'] !== undefined && data['routes'] !== null && data['routes'].length > 0)
    ) {
        if (!data.routes) {
            config.push(obj);
        }
        let routes = data["routes"];
        for (let index in routes) {
            let itemData = routes[index];
            getConfig(path, itemData, config);
        }
    }
}

